package sparrowGui
{
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextFieldAutoSize;
	import flash.utils.getTimer;
	
	import sparrowGui.utils.TextFieldEx;

	/**
	 * 基本提示框,只有点击消失和出现坐标调整的属性
	 * @author Pelephone
	 */	
	public class BaseTip extends ComponentDraw
	{
		public static const TIPS_POSI_AUTO:int = 0;	//0.提示框自动根据边距出现在鼠标上下左右
		public static const TIPS_POSI_RD:int = 1;	//1.右下
		public static const TIPS_POSI_RU:int = 2;	//2.右上
		public static const TIPS_POSI_LU:int = 3;	//3.左上
		public static const TIPS_POSI_LD:int = 4;	//4.左下
		public static const TIPS_POSI_UM:int = 5;	//上居中
		public static const TIPS_POSI_DM:int = 6;	//下居中
		public static const TIPS_POSI_M_AUTO:int = 10;	//自动居中上下提示
		
		public static const TIPS_UPDATA:String = "TIPS_UPDATA";		//组件内容更新
		
//		protected static var instance : TipsRender;
		protected var _parent:DisplayObjectContainer;
//		private var _txt:TextFieldEx;
		protected var _btns:Array;
		protected var _Datas:Array;
		
//		private var _delayTime:int = 1000;
		protected var _overObj:InteractiveObject;		// 经过的对象
		
//		// 延迟多少毫秒出现提示框
//		public var delay:int;
//		// 类型,0.表示出现的提示框自动根据边距出现在鼠标上下左右处.1.右下,2.右上,3.左上,4.左下
//		public var showType:int;
//		public var spacing:int;
		
		public function BaseTip(parent:DisplayObjectContainer,vars:Object=null)
		{
			 _parent = parent;
			 _btns = [];
			 _Datas = [];
			 
//			 _delayTime = delay;
			
			vars = vars || SparrowGUI.getIns().tipVar;
			super(vars);
			
//			delay = vars.delay;
//			showType = vars.showType;
//			spacing = vars.spacing;
			
//			if (instance != null) throw Error("此单例已经存在了");
//			instance = this;
		}
		
		/**
		 * 初始放入的容器
		 * @param parent
		 */		
		override protected function init():void
		{
/*			_txt = new TextFieldEx();
			addChild(_txt);
//			_parent.addChild(this);
			this.visible = false;
			this.mouseChildren = this.mouseEnabled = false;
			_txt.textField.textColor = 0xFFFFFF;
			if(!style.height){
				_txt.textField.autoSize = TextFieldAutoSize.LEFT;
				_txt.textField.wordWrap = false;
			}*/
		}
		
		override protected function draw():void
		{
			super.draw();

/*			_txt.x = style.paddingLeft;
			_txt.y = style.paddingTop;
			
			if(style.width) _txt.width = style.width - style.paddingLeft - style.paddingRight;
			if(style.height) _txt.height = style.height - style.paddingTop - style.paddingBottom;*/
		}
		
		public function update(data:Object):void
		{
			dispatchEvent(new Event(TIPS_UPDATA));
		}
		
		protected function onEvt(e:Event):void
		{
		}
		
		protected function addEvt():void
		{
			removeEvt();
			if(!_parent || !_parent.stage) return;
			
			_parent.stage.addEventListener(MouseEvent.MOUSE_UP,onEvt);
			_parent.stage.addEventListener(Event.MOUSE_LEAVE,onEvt);
		}
		
		protected function removeEvt():void
		{
			if(!_parent || !_parent.stage) return;
			_parent.stage.removeEventListener(MouseEvent.MOUSE_UP,onEvt);
			_parent.stage.removeEventListener(Event.MOUSE_LEAVE,onEvt);
		}
		
		/**
		 * 设置提示框位置,经过时判断一次位置
		 * 屏幕的左上右下过界的判断
		 */		
		protected function setTipPosi(iobj:InteractiveObject):void
		{
			var localPt:Point = iobj.localToGlobal(new Point(0,0));
			var px:int = localPt.x;
			var py:int = localPt.y;
			var showType:int = style.showType;
			var spacing:int = style.spacing;
			
			var swidth:int = SparrowGUI.stageWidth;
			var sheight:int = SparrowGUI.stageHeight;
			
			// 复杂的坐标算法
			if(((!iobj || showType==TIPS_POSI_AUTO)
				&& ((px + spacing + iobj.width + this.width)>swidth))
				|| showType==TIPS_POSI_LU || showType==TIPS_POSI_LD)
				this.x = (px - spacing - this.width);
			else if(iobj && (showType==TIPS_POSI_UM || showType==TIPS_POSI_DM || showType==TIPS_POSI_M_AUTO))
				this.x = px - this.width/2 + iobj.width/2;
			else
				this.x = (px + iobj.width + spacing);
			
			// y轴是跟据底部边距判断是否出界,出界就以底部参考调整位置,反之以iobj为参考
			if(!iobj || showType==TIPS_POSI_AUTO || showType==TIPS_POSI_DM || showType==TIPS_POSI_UM)
			{
				if((py + spacing + iobj.height + this.height)>sheight && showType!=TIPS_POSI_DM && showType!=TIPS_POSI_UM)
					this.y = (sheight - spacing - this.height);
				else if(showType==TIPS_POSI_DM || showType==TIPS_POSI_AUTO)
					this.y = (py + spacing + iobj.height);
				else
					this.y = (py - spacing - this.height);
			}
			else
			{
				this.y = (py + iobj.height + spacing);
			}
			
/*			if(((!iobj || showType==TIPS_POSI_AUTO)
				&& ((py + spacing + iobj.height + this.height)>sheight))
				|| showType==TIPS_POSI_RU || showType==TIPS_POSI_LU || showType==TIPS_POSI_UM)
				this.y = (py - spacing - this.height);
			else
				this.y = (py + iobj.height + spacing);*/
		}
		
		/**
		 * 设置提示框位置,经过时判断一次位置
		 * 屏幕的左上右下过界的判断
		 	
		private function setTipPosi(ot:TipsTmp):void
		{
			var px:int = _parent.mouseX;
			var py:int = _parent.mouseY;
			
			if(((!ot || showType==TIPS_POSI_AUTO) && ((px + spacing + this.width)>_parent.stage.stageWidth))
				|| showType==TIPS_POSI_LU || showType==TIPS_POSI_LD)
				this.x = (px - this.width - spacing);
			else
				this.x = (px + spacing);
			
			if(((!ot || showType==TIPS_POSI_AUTO) && ((py + spacing + this.height)>_parent.stage.stageHeight))
				|| showType==TIPS_POSI_RU || showType==TIPS_POSI_LU)
				this.y = (py - this.height - spacing);
			else
				this.y = (py + spacing);
		}*/	
		
		/**
		 * 按钮激活,使其有鼠标经过数秒后弹出提示窗.
		 * @param sp		经过要提示的对象
		 * @param tipTxt	经过提示的文字
		 * @param delay		延迟多少毫秒出现提示框
		 * @param type		类型,0.表示出现的提示框自动根据边距出现在鼠标上下左右处.1.右下,2.右上,3.左上,4.左下
		 */
		public function addTipBtn(tipObj:InteractiveObject,tipData:Object):void
		{
			_btns.push(tipObj);
			_Datas.push(tipData);
//			_btns[sp] = tipData;
		}
		
		/**
		 * 移除本组件里带提示框的所有对象和事件
		 */		
		public function unActAllBtn():void
		{
			for each(var ino:InteractiveObject in _btns){
				if(!ino) continue;
				killTip(ino);
			}
		}
		
		public function getTipObj(tipObj:InteractiveObject):Object
		{
//			return _btns[tipObj];
			return _Datas[_btns.indexOf(tipObj)];
		}
		
		public function get overTip():InteractiveObject
		{
			return _overObj;
		}
		
		/**
		 * 清除对象上的事件
		 * @param tipObj
		 */		
		public function killTip(tipObj:InteractiveObject):void
		{
			if(!tipObj) return;
//			_btns[tipObj] = null;
//			delete _btns[tipObj];
			var tid:int = _btns.indexOf(tipObj);
			if(tid>=0)
			{
				_btns.splice(tid,1);
				_Datas.splice(tid,1);
			}
		}
		
		/**
		 * 清除不在舞台上的所有对象的overtip
		 */		
		public function removeNoStageEvt():void
		{
			for each(var tip:InteractiveObject in _btns){
				if(tip.stage) continue;
				killTip(tip);
			}
		}
		
		/**
		 * 获取文本框
		 * @return 
		 		
		public function get tf():TextFieldEx
		{
			return _txt;
		}*/
		
		/**
		 * 作为文本字段中当前文本的字符串。各行之间用回车符（'\r'，即 ASCII 13）分隔。此属性包含文本字段中的无格式文本，不带 HTML 标签。 
		 * @param value
		 	
		public function set text(value:String):void
		{
			_txt.textField.text = value;
			style.width = _txt.width + style.paddingLeft + style.paddingRight;
			style.height = _txt.height + style.paddingBottom + style.paddingTop;
			nextDraw();
		}*/
		
		/**
		 * 包含文本字段内容的 HTML 表示形式。 
		 * @param value
		 
		public function set htmlText(value:String):void
		{
			_txt.htmlText = value;
			style.width = _txt.width + style.paddingLeft + style.paddingRight;
			style.height = _txt.height + style.paddingBottom + style.paddingTop;
			nextDraw();
		}*/
	}
}
import flash.display.InteractiveObject;

class TipsTmp
{
	public var Iobj:InteractiveObject;
//	public var Itip:String;
	public var voData:Object;		//非文字对象
}